package com_.dxy_test_.drawseal;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

// ★★★来源资料
// https://www.cnblogs.com/kikyoqiang/p/14283366.html
public class DrawByOpencv {
    private static String source = "C:\\Users\\Alex\\Desktop\\111.jpg";
    private static String target = "C:\\Users\\Alex\\Desktop\\222.jpg";

    public static void main(String[] args) throws Exception {
        drawRoundRect();
    }

    //画线条
    private static void drawLine() throws Exception {
        BufferedImage image = ImageIO.read(new File(source));
        Graphics2D g2d = (Graphics2D) image.getGraphics();
        g2d.setStroke(new BasicStroke(5));//BasicStroke(线条粗细)
        g2d.setColor(Color.red);
        //drawLine(int x1,int y1,int x2,int y2)
        g2d.drawLine(100, 100, 100, 500);//画一条线段
        // g2d.drawLine(100, 100, 100, 100);//画一个点。
        FileOutputStream out = new FileOutputStream(target);
        ImageIO.write(image, "jpeg", out);
    }

    //画普通矩形
    private static void drawRect() throws Exception {
        BufferedImage image = ImageIO.read(new File(source));
        Graphics2D g2d = (Graphics2D) image.getGraphics();
        g2d.setStroke(new BasicStroke(5));//BasicStroke(线条粗细)
        g2d.setColor(Color.red);
        g2d.drawRect(200, 200, 300, 200);//画线框drawRect(int x,int y,int width,int height)
        FileOutputStream out = new FileOutputStream(target);
        ImageIO.write(image, "jpeg", out);
    }

    //画圆角矩形
    private static void drawRoundRect() throws Exception {
        BufferedImage image = ImageIO.read(new File(source));
        Graphics2D g2d = (Graphics2D) image.getGraphics();
        g2d.setStroke(new BasicStroke(5));//BasicStroke(线条粗细)
        g2d.setColor(Color.red);
        g2d.drawRoundRect(200, 200, 300, 100, 20, 20);
        FileOutputStream out = new FileOutputStream(target);
        ImageIO.write(image, "jpeg", out);
    }

    //画三维矩形
    private static void draw3DRect() throws Exception {
        BufferedImage image = ImageIO.read(new File(source));
        Graphics2D g2d = (Graphics2D) image.getGraphics();
        g2d.setStroke(new BasicStroke(5));//BasicStroke(线条粗细)
        g2d.setColor(Color.red);
        g2d.draw3DRect(80, 100, 40, 25, true);//画一个线框
        g2d.fill3DRect(20, 70, 20, 30, true);//画一个着色块
        FileOutputStream out = new FileOutputStream(target);
        ImageIO.write(image, "jpeg", out);
    }

    //画椭圆形
    private static void drawOval() throws Exception {
        BufferedImage image = ImageIO.read(new File(source));
        Graphics2D g2d = (Graphics2D) image.getGraphics();
        g2d.setStroke(new BasicStroke(5));//BasicStroke(线条粗细)
        g2d.setColor(Color.red);
        g2d.drawOval(10, 10, 60, 120);//画椭圆
        g2d.setColor(Color.cyan);
        g2d.fillOval(100, 30, 60, 60);//涂圆块
        g2d.setColor(Color.magenta);
        g2d.fillOval(15, 140, 100, 50);//涂椭圆
        FileOutputStream out = new FileOutputStream(target);
        ImageIO.write(image, "jpeg", out);
    }

    //画圆弧
    private static void drawArc() throws Exception {
        BufferedImage image = ImageIO.read(new File(source));
        Graphics2D g2d = (Graphics2D) image.getGraphics();
        g2d.setStroke(new BasicStroke(5));//BasicStroke(线条粗细)
        g2d.setColor(Color.red);
        g2d.drawArc(10, 40, 90, 50, 0, 180);//画圆弧线
        g2d.drawArc(100, 40, 90, 50, 180, 180);//画圆弧线
        g2d.setColor(Color.yellow);
        g2d.fillArc(10, 100, 40, 40, 0, -270);//填充缺右上角的四分之三的椭圆
        g2d.setColor(Color.green);
        g2d.fillArc(60, 110, 110, 60, -90, -270);//填充缺左下角的四分之三的椭圆
        FileOutputStream out = new FileOutputStream(target);
        ImageIO.write(image, "jpeg", out);
    }

    //画多边形
    private static void drawPolygon() throws Exception {
        BufferedImage image = ImageIO.read(new File(source));
        Graphics2D g2d = (Graphics2D) image.getGraphics();
        g2d.setStroke(new BasicStroke(5));//BasicStroke(线条粗细)
        g2d.setColor(Color.red);
        int px1[] = {50, 90, 10, 50};//首末点相重,才能画多边形
        int py1[] = {10, 50, 50, 10};
        int px2[] = {140, 180, 170, 180, 140, 100, 110, 140};
        int py2[] = {5, 25, 35, 45, 65, 35, 25, 5};
        g2d.setColor(Color.blue);
        g2d.fillPolygon(px1, py1, 4);
        g2d.setColor(Color.red);
        g2d.drawPolygon(px2, py2, 8);
        FileOutputStream out = new FileOutputStream(target);
        ImageIO.write(image, "jpeg", out);
    }

    //擦除矩形块
    private static void clearRect() throws Exception {
        BufferedImage image = ImageIO.read(new File(source));
        Graphics2D g2d = (Graphics2D) image.getGraphics();
        g2d.setStroke(new BasicStroke(5));//BasicStroke(线条粗细)
        g2d.setColor(Color.red);
        g2d.setColor(Color.blue);
        g2d.fillOval(50, 50, 100, 100);
        g2d.clearRect(70, 70, 40, 55);
        FileOutputStream out = new FileOutputStream(target);
        ImageIO.write(image, "jpeg", out);
    }

    //限定作图显示区域
    private static void clipRect() throws Exception {
        BufferedImage image = ImageIO.read(new File(source));
        Graphics2D g2d = (Graphics2D) image.getGraphics();
        g2d.setStroke(new BasicStroke(5));//BasicStroke(线条粗细)
        g2d.setColor(Color.red);
        g2d.clipRect(0, 0, 100, 50);
        g2d.clipRect(50, 25, 100, 50);
        FileOutputStream out = new FileOutputStream(target);
        ImageIO.write(image, "jpeg", out);
    }

    //复制图形
    private static void copyArea() throws Exception {
        BufferedImage image = ImageIO.read(new File(source));
        Graphics2D g2d = (Graphics2D) image.getGraphics();
        g2d.setStroke(new BasicStroke(5));//BasicStroke(线条粗细)
        g2d.drawRect(10, 10, 60, 90);
        g2d.fillRect(90, 10, 60, 90);
        g2d.copyArea(40, 50, 60, 70, -20, 80);
        g2d.copyArea(110, 50, 60, 60, 10, 80);
        FileOutputStream out = new FileOutputStream(target);
        ImageIO.write(image, "jpeg", out);
    }


}
